package com.cn.por.util.pageUtils;

import cn.hsa.base.web.vo.PageResultData;
import cn.hutool.core.util.PageUtil;

import java.util.List;

/**
 * 对数据进行分页处理，返回分页对象
 * 👍👍👍👍👍👍
 */
public class PageUtilCustom<T> {

    public static <T> PageResultData<T> getPageDto(List<T> list, int pageNum, int pageSize) {
        //总页数
        int pages = PageUtil.totalPage(list.size(), pageSize);
        //开始位置和结束位置
        int[] startEnd = PageUtil.transToStartEnd(pageNum - 1, pageSize);
        //分页后的结果集
        List<T> pageList = null;
        if (startEnd[1] < list.size()) {
            pageList = list.subList(startEnd[0], startEnd[1]);
        } else {
            pageList = list.subList(startEnd[0], list.size());
        }
        PageResultData page = new PageResultData();
        page.setRowCount((long) list.size());
        page.setRows(pageList);
        page.setPageStart((long) pageNum);
        page.setPageSize((long) pageSize);
        page.setPageCount((long) pages);
        return page;
    }
}
